//
//  EntranceViewController.m
//  j绝版新闻客户端
//
//  Created by weige on 16/5/8.
//  Copyright © 2016年 weige. All rights reserved.
//

#import "GuideViewController.h"
#import "GuideCell.h"
@interface GuideViewController ()
{
    UICollectionView *collectionView;
    NSArray *imgArr;
    NSString *cellIdentifier;
    BOOL isHiddenNextButton;
    UIPageControl *pageController;
    
}
@end

@implementation GuideViewController

static NSString * const reuseIdentifier = @"Cell";
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadUIDidView];
    [self pageControllerView];
}

-(void)loadUIDidView
{
    imgArr=@[@"guide_40_1", @"guide_40_2", @"guide_40_3", @"guide_40_4"];
    //创建一个流失布局
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
    //设置大小
    flowLayout.itemSize=ScreenBounds.size;
    //滚动方向
    flowLayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    //flowLayout.sectionInset=UIEdgeInsetsZero;
    flowLayout.minimumInteritemSpacing=0;
    flowLayout.minimumLineSpacing=0;
    collectionView=[[UICollectionView alloc]initWithFrame:ScreenBounds collectionViewLayout:flowLayout];
    collectionView.delegate=self;
    collectionView.dataSource=self;
    collectionView.showsHorizontalScrollIndicator=NO;
    collectionView.showsVerticalScrollIndicator=NO;
    collectionView.bounces=NO;
    collectionView.pagingEnabled=YES;
    
    [collectionView registerClass:GuideCell.self forCellWithReuseIdentifier:reuseIdentifier
     ];
    
    pageController=[[UIPageControl alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 20)];
    [self.view addSubview:collectionView];
    NSLog(@" 测数据哦");
}

-(void)pageControllerView
{
    pageController.numberOfPages=imgArr.count;
    pageController.currentPage=0;
    [self.view addSubview:pageController];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return imgArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionVieww cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GuideCell *cell=[collectionVieww dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.imageView.image=[UIImage imageNamed:imgArr[indexPath.row]];
    
    return cell;
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   // NSLog(@"1=%f",scrollView.contentOffset.x);
   // NSLog(@"2=%f",(CGFloat)SCREEN_WIDTH*(imgArr.count-1));
    GuideCell *cell=(GuideCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath    indexPathForRow:imgArr.count-1 inSection:0]];
    if (scrollView.contentOffset.x==(CGFloat)SCREEN_WIDTH*(imgArr.count-1)) {
        
        NSLog(@"%@",cell);
        [cell setNextButtonHidden:NO];
        ;
    }
    isHiddenNextButton=NO;

}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"%d,%d",!isHiddenNextButton,isHiddenNextButton);
    
    if (scrollView.contentOffset.x!=(CGFloat)SCREEN_WIDTH*(imgArr.count-1)&&!isHiddenNextButton&&scrollView.contentOffset.x>(CGFloat)SCREEN_WIDTH*(imgArr.count-2)) {
        GuideCell *cell=(GuideCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath    indexPathForRow:imgArr.count-1 inSection:0]];

        [cell setNextButtonHidden:YES];
        
        isHiddenNextButton=YES;
        
        
        
    }
    pageController.currentPage=scrollView.contentOffset.x/SCREEN_WIDTH;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
